//
//  HLJDetailViewController.m
//  B段项目
//
//  Created by 111111 on 15/12/22.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "HLJDetailViewController.h"

#import "HLJHomeDetailModel.h"
@interface HLJDetailViewController ()<UIWebViewDelegate>
@property(nonatomic,retain)UIWebView *webView;
@property(nonatomic, retain)HLJRefesh *refesh;
@end

@implementation HLJDetailViewController

-(void)viewWillAppear:(BOOL)animated{
       [self requestData];
}


#pragma mark - ViewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    

    
    
    
    self.titleLabel.size = CGSizeMake(320, 30);
   // self.titleLabel.backgroundColor = [UIColor yellowColor];
    self.titleLabel.text = self.titleStr;
    
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0,WIDTH , HEIGHT)];
    self.webView.delegate = self;
    self.webView.scrollView.bounces = NO;
    [self.view addSubview:self.webView];
  
    
}


#pragma mark --  webView自适应

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
        
    //字体大小
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '330%'"];
    //    //字体颜色
    //    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
    //    //页面背景色
    //    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
    
    
    NSString *str1 = [NSString stringWithFormat:@"%0.2f",WIDTH-20];
    
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:
                                                     @"var script = document.createElement('script');"
                                                     "script.type = 'text/javascript';"
                                                     "script.text = \"function ResizeImages() { "
                                                     "var myimg,oldwidth;"
                                                     "var maxwidth= %@;"//缩放系数,调动这个参数可以改变图片的宽度
                                                     "for(i=0;i <document.images.length;i++){"
                                                     "myimg = document.images[i];"
                                                     "oldwidth = myimg.width;"
                                                     "if (oldwidth >= 300) {"
                                                     "myimg.width = maxwidth;"
                                                     "myimg.height = myimg.height;"
                                                     "}"
                                                     "}"
                                                     "}\";"
                                                     "document.getElementsByTagName('head')[0].appendChild(script);",str1]];
    
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";
    [webView stringByEvaluatingJavaScriptFromString:str];
    // NSLog(@"22222222");
    
    
}



#pragma mark --请求数据

-(void)requestData{
    
//    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    [hub show:YES];
//    hub.labelText = @"正在为您加载.....";
//    [self.view addSubview:hub];
    self.refesh = [[HLJRefesh alloc]init];
    self.refesh.size = CGSizeMake(50, 50);
    self.refesh.center = CGPointMake(self.view.width / 2, (self.view.height - 80) / 2);
    [self.view addSubview:self.refesh];
    [self.view bringSubviewToFront:self.refesh];
    NSString *urlStr = @"http://api2.pianke.me/article/info";
    NSString *body = [NSString stringWithFormat: @"auth=&client=1&contentid=%@&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&version=3.0.6",self.detaId];
    
    //auth=W81oFCvlm3m3hDCsN8G8bskDJxQMSiNeWLoD1sXzE2cWP22N1JSh9b6P4&client=1&contentid=%@&deviceid=EC4A37B3-25F3-4560-BDE7-AFB0EB45C576&version=3.0.6
    
    [ZYTools postUrl:urlStr body:body result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
      //  NSLog(@"%@",result);
        
                NSMutableDictionary *dic = [result objectForKey:@"data"];
       
        
                NSString *htmlStr = [dic objectForKey:@"html"];
        
        
                [self.webView loadHTMLString:htmlStr baseURL:nil];
       // NSLog(@"%@",htmlStr);
               // [hub hide:YES];
        [self.refesh removeFromSuperview];
        
        
    } failure:^(NSError *error) {
       // [hub hide:YES];

        [self.refesh removeFromSuperview];
    }];

    
    
}




#pragma mark -- 消除滚动轮指示器
-(void)hideWaiting
{
    [[self.view viewWithTag:9998] removeFromSuperview];
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
